/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jheuristics;

import java.io.Serializable;
import java.util.List;

import org.epanetgrid.otimizacao.util.OtimizacaoUtil;
import org.jheuristics.ga.exception.IllegalIndividualException;
import org.jheuristics.util.BitString;


/**
 * Uma implementa��o padr�o da interface <code>Individual</code>.
 *
 * <p>Note: this class has a <i>natural ordering</i> that is <i>inconsistent with equals</i>.
 * See {@link Comparable} for more information.
 *
 * @see org.jheuristics.Individual
 */
public class DefaultIndividual implements Individual, Serializable {
	private Object gens;
	private Comparable fitness;
	private Object dataApplication;

	/**
	 * Construtor padr�o.
	 *
	 * @param gens Um objeto representando a solu��o candidata diferente de <code>null</code>.
	 */
	public DefaultIndividual(Object gens) {
		if (null == gens) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		if (!validate(gens)) {
			throw new IllegalIndividualException();
		}
		this.gens = gens;
		fitness = null;
		dataApplication = null;
	}

	/**
	 * Retorna os objetos que o usu�rio armazenou relativos a este indiv�duo.
	 *
	 * @return Os objetos armazenados relativos a este indiv�duo.
     * @see org.jheuristics.Individual#getDataApplication()
	 */
    public Object getDataApplication() {
        return dataApplication;
    }

	/**
	 * Atribui objetos relacionados a este indiv�duo.
	 *
	 * @param object O objeto a ser armazenado.
     * @see org.jheuristics.Individual#setDataApplication(java.lang.Object)
     */
    public void setDataApplication(Object object) {
        dataApplication = object;
    }

	/**
	 * Retorna o cromossomo ou genes que representa a solu��o canditada desse indiv�duo.
	 *
	 * @return Uma representa��o da solu��o canditada.
	 * @see org.jheuristics.Individual#getGens()
	 */
	public Object getGens() {
		return gens;
	}

	/**
	 * Retorna uma forma de compara��o entre diferentes solu��es. 
	 * Geralmente � utilizado n�meros (e.g. <code>Number</code>).
	 *
	 * @return Um forma de compara��o entre diferentes solu��es.
	 * @see org.jheuristics.Individual#getFitness()
	 */
	public Comparable getFitness() {
		return fitness;
	}

	/**
	 * Atribui uma forma de compara��o entre diferentes solu��es.
	 *
	 * @param fitness O fitness desse indiv�duo.
	 * @see org.jheuristics.Individual#setFitness(java.lang.Comparable)
	 */
	public void setFitness(Comparable fitness) {
		this.fitness = fitness;
	}

    /**
     * Compara dois indiv�duos de acordo com o fitness.
     *
     * @param obj O outro indiv�duo.
     * @return <code>-1</code> se este indiv�duo � pior que o outro, <code>1</code> se este indiv�duo � melhor que o outro, <code>0</code> caso contr�rio.
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Object obj) {
        Comparable otherFit = ((Individual) obj).getFitness();
        if (null == fitness) {
            if (null == otherFit) {
                return 0;
            }
            return -1;
        }
        if (null == otherFit) {
            return +1;
        }
        return fitness.compareTo(otherFit);
    }

    /**
     * Compara se dois indiv�duos s�o iguais.
     *
     * @param that O outro indiv�duo.
     * @return <code>true</code> se os dois indiv�duos possuem os mesmos genes, <code>false</code> caso contr�rio.
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object that) {
        if (this == that)
            return true;
        if (that == null || that.getClass() != this.getClass()) 
            return false;
        DefaultIndividual other = (DefaultIndividual) that;
        return gens.equals(other.gens);
    }

    /**
     * Retorna um n�mero hash para este indiv�duo.
     *
     * @return O n�mero hash deste indiv�duo.
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return gens.hashCode();
    }
    
    /**
     * Este m�todo � um ponto de extens�o.
     * Se existir uma configura��o nos gens ou cromossomo este m�todo deve retornar <code>false</code>.
     *
     * @param gens O cromossomo ou genes que representa a solu��o canditada desse indiv�duo.
     * @return <code>true</code> se a configura��o dos genes deve ser aceita, <code>false</code> caso contr�rio.
     */
    protected boolean validate(Object gens) {
    	return true;
    }

    /* Retorna a lista de gens
     * @see java.lang.Object#toString()
     */
    public String toString() {
    	if(gens instanceof List) {
    	return OtimizacaoUtil.gensToString((List<BitString>) this.getGens());
    	}else {
    		return this.gens.toString();
    	}
    }
    
}
